package tech.xiaozai.rbac.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-09-16 10:50
 *
 *  自定义的跨域过滤器
 */
//@WebFilter("/*")
public class CorsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;


        System.out.println("请求到来:  url="+req.getRequestURI());
        System.out.println("请求方法:  method="+req.getMethod());

        /**
         *
         *   去了解浏览器的同源策略和跨域
         *  add_header 'Access-Control-Allow-Origin' '*';
         *         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
         *         add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         *
         */


        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","*");
        response.setHeader("Access-Control-Allow-Headers","*");

        filterChain.doFilter(servletRequest,servletResponse);

    }
}
